﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace SEO.Utility
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 下载类，指定地址下载文件
    /// 完成日期：2011年4月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class Download
    {
        /// <summary>
        /// 下载文件类
        /// </summary>
        /// <param name="strURL"></param>
        /// <param name="strFilename"></param>
        /// <param name="prog"></param>
        public void DownloadFile(string strURL, string strFilename, System.Windows.Forms.ProgressBar prog)
        {
            try
            {
                System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(strURL);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;
                if (prog != null)
                {
                    prog.Maximum = (int)totalBytes;
                }
                System.IO.Stream st = myrp.GetResponseStream();
                System.IO.Stream so = new System.IO.FileStream(strFilename, System.IO.FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    System.Windows.Forms.Application.DoEvents();
                    so.Write(by, 0, osize);
                    if (prog != null)
                    {
                        prog.Value = (int)totalDownloadedByte;
                    }
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
            }
            catch (System.Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取最新版本
        /// </summary>
        /// <returns></returns>
        public static string GetNewVersion()
        {
            string strVersion = "";

            try
            {
                string strURL = "http://login.quanlidao.com/VersionZXG.aspx";
                SEO.Utility.WebClient client = new SEO.Utility.WebClient();
                client.Encoding = System.Text.Encoding.GetEncoding("gb2312");
                client.OpenRead(strURL);
                strVersion = client.RespHtml;
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("获取历史版本 -> " + ex.Message);
            }

            return strVersion;
        }

    }
}
